<template>
  <div class="m-box" @mouseup="mouseup">
    <div class="m-mibox">
      <span class="m-span1">
        <a>个人设置</a>
        <span>>更换头像</span>
      </span>

      <div class="m-span2">
        <el-button>上传头像</el-button>
        <span>支持jpg、png、bmp格式的图片，且文件小于5M</span>
      </div>

      <div class="m-span3">
        <div class="m-l">
          <div class="ztag" ref="ztagref" @mousemove="mousemovebox">
            <div class="m-too" ref="too" @mousedown.self="mouserdownbox">
              <div class="zoom"></div>
            </div>
            <img class="bgimg" src="@/assets/image/3.png" alt="" />

            <img
              ref="tooimg"
              class="opaciimg"
              src="@/assets/image/3.png"
              alt=""
            />
          </div>
        </div>

        <div class="m-r">
          <span class="mr-spans">头像预览</span>
          <div class="mr-img1">
            <img src="@/assets/image/3.png" alt="" />
          </div>
          <span class="mr-span">大尺寸头像(180*180像素)</span>
          <div class="mr-img2">
            <img src="@/assets/image/3.png" alt="" />
          </div>
          <span class="mr-span">大尺寸头像(40*40像素)</span>
        </div>
      </div>
    </div>

    <!-- 测试 -->
    <div class="cropper-box">
      <vueCropper
        ref="cropper"
        :img="option.img"
        :outputSize="option.size"
        :outputType="option.outputType"
        :autoCrop="true"
        autoCropWidth="50%"
        :canScale="false"
      ></vueCropper>
      
    </div>
  </div>
</template>

<script>
import { VueCropper } from "vue-cropper";

export default {
  data() {
    return {
      land: false,
      //   测试
      option: {
        img: "https://avatars2.githubusercontent.com/u/15681693?s=460&v=4",
        size: 1,
        outputType: "jpg",
      },
      previews: {},
      previewStyle2: {},
    };
  },
  components: {
    VueCropper,
  },
  methods: {
    mouserdownbox(e) {
      this.land = true;
    },
    mousemovebox(e) {
      if (this.land) {
        let ztagref = this.$refs.ztagref;
        var x = e.pageX - ztagref.offsetLeft;
        var y = e.pageY - ztagref.offsetTop;
        //   大框框
        //   第二个是y
        console.log(x, y);

        let too = this.$refs.too;

        var width = x - too.offsetWidth / 2;
        var height = y - too.offsetHeight / 2;

        if (width <= 0) {
          width = 0;
        } else if (width >= ztagref.offsetWidth - too.offsetWidth) {
          width = 150;
        }
        if (height <= 0) {
          height = 0;
        } else if (height >= ztagref.offsetHeight - too.offsetHeight) {
          height = ztagref.offsetHeight - too.offsetHeight;
        }
        let mousew = this.$refs.too.offsetWidth + height + "px";
        let mouseh = this.$refs.too.offsetHeight + width + "px";
        this.$refs.tooimg.style.clip = `rect(${height}px, ${mouseh}, ${mousew}, ${width}px)`;
        too.style.top = height + "px";
        too.style.left = width + "px";
      }
    },
    mouseup() {
      this.land = false;
    },
    realTime(data) {
      var previews = data;
      var h = 0.5;
      var w = 0.2;
      this.previewStyle2 = {
        width: previews.w + "px",
        height: previews.h + "px",
        overflow: "hidden",
        margin: "0",
        zoom: w,
      };
    },
  },
};
</script>

<style lang="less" scoped>
.m-box {
  width: 980px;
  background-color: #fff;
  min-height: 500px;
  height: 1000px;
  border: 1px solid rgb(179, 177, 177);
  //   motion: 0 auto;
  margin: 0 auto;
  .m-mibox {
    padding-top: 30px;
    width: 900px;
    margin: 0 auto;
    .m-span1 {
      width: 100%;
      height: 30px;
      display: block;
      border-bottom: 1px solid #dbdbdb;
      font-size: 18px;
      a {
        color: #0c89d4;
      }
    }
    .m-span2 {
      margin-top: 30px;
      span {
        font-size: 12px;
        display: inline;
        margin-left: 13px;
        color: #999999;
      }
    }
    .m-span3 {
      margin-top: 30px;
      display: flex;
      .mr-spans {
        display: block;
        margin-bottom: 10px;
        font-size: 12px;
      }
      .m-l {
        width: 322px;
        height: 322px;
        border: solid 1px #d1d1d1;
        background-color: #f6f6f6;
        margin-right: 50px;
        // border-right: #999999 solid 1px;
        .ztag {
          position: relative;

          width: 322px;
          height: 322px;
          top: 0px;
          left: 0px;
          img {
            width: 100%;
            height: 100%;
          }

          .m-too {
            width: 180px;
            height: 180px;
            border: 1px solid #fff;
            z-index: 10;
            // left: 10px;
            // top: 100px;
            // background-image: url();
            // background: rgba(255, 255, 255, 0.5);
            zoom: 1;
            cursor: move;
            position: absolute;
            .zoom {
              width: 6px;
              height: 6px;
              right: -1px;
              bottom: -1px;
              position: absolute;
              border: 1px solid #fff;
              cursor: nw-resize;
              background: #000;
              opacity: 0.8;
              //   opacity.80);
              overflow: hidden;
            }
          }
          .bgimg {
            opacity: 0.5;
          }
          .opaciimg {
            width: 100%;
            height: 100%;
            left: 0;
            position: absolute;
            //  距离上边  距离宽 距离高 距离左边
            clip: rect(0px, 180px, 180px, 0px);
          }
        }
      }
      .m-r {
        border-left: #bdbaba solid 1px;
        padding-left: 50px;
        .mr-img1 {
          img {
            width: 140px;
            height: 140px;
          }
        }
        .mr-span {
          font-size: 12px;
          color: #666;
          display: block;
          margin: 10px auto;
        }
        .mr-img2 {
          img {
            width: 40px;
            height: 40px;
          }
        }
      }
    }
  }
}
.cropper-box {
  width: 322px;
  height: 322px;
}
</style>
